#include "light.h"




AppLightPage::AppLightPage(Page* parent) : AppPage(parent)
{

  this->viewport = new Container(this);
  this->viewport->set_size(220, 260);
  this->viewport->set_bg_color(lv_color_hex(THEME_COLOR_GRAY_7), 255, 0);
  this->viewport->set_flex_layout(LV_FLEX_FLOW_COLUMN, false, false, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);


  this->btn_switch = new ButtonSwitch(this->viewport);
  this->btn_switch->set_size(80, 80);
  this->btn_switch->set_icon(&ui_img_light_png);

  this->btn_switch->on_switch_change([](ButtonSwitch::change_t status, void* user_data) {

    AppLightPage* app_light_page = (AppLightPage*) user_data;

    app_light_page->btn_switch->set_ui_state(true);
    app_light_page->btn_switch->hidden(true);
    app_light_page->viewport->set_bg_color(lv_color_hex(THEME_COLOR_WHITE_PURE), 255, 0);

    app_light_page->is_light_on = true;

    }, this);

  this->viewport->set_event_cb([](lv_event_t* e) {

    lv_event_code_t  code = lv_event_get_code(e);

    AppLightPage* app_light_page = (AppLightPage*) e->user_data;

    if (app_light_page->is_light_on || code == LV_EVENT_CLICKED)
    {

      app_light_page->btn_switch->set_ui_state(false);
      app_light_page->btn_switch->hidden(false);
      app_light_page->viewport->set_bg_color(lv_color_hex(THEME_COLOR_GRAY_7), 255, 0);
      app_light_page->is_light_on = false;

    }

    }, LV_EVENT_CLICKED, this);





}


AppLightPage::~AppLightPage()
{

  delete this->viewport;
  delete this->btn_switch;
}





void app_light_main(void* data)
{
  Page* page = (Page*) data;

  AppLightPage* app_light_page = new AppLightPage(page);
  app_light_page->show();

}


void app_light_init()
{
  app_manger.install("light", app_light_main, NULL, &ui_img_light_png, lv_color_hex(THEME_COLOR_ORANGE));
}

